package com.lp.multi.juc.collection;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author: lp
 * @create: 2021-06-27 15:52
 * @description:
 * Callable和Runner区别
 * 1.Callable可以有返回值
 * 2.Callable可以抛出返回值
 * 3.Callable运行方法是call,Runner运行的方法是run
 *
 * 打印结果
 * A call被调用
 * 1024
 *
 */
public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //怎么启动Callable
        CallThread callThread = new CallThread();
        FutureTask futureTask = new FutureTask(callThread);
        new Thread(futureTask,"A").start();
        // # TODO B线程并不会执行call()方法
        // FutureTask源码分析  state存状态,outcome存结果.run方法：if (state != NEW ||...) return;
        new Thread(futureTask,"B").start();

        //获取结果：get是阻塞方法
        System.out.println(futureTask.get());

    }

}

class CallThread implements Callable<String>{

    @Override
    public String call() throws Exception {
        System.out.println(Thread.currentThread().getName()+" call被调用");
        return "1024";
    }
}
